<!--
    -  Copyright (c) 2018-2025, haohanwork.com All rights reserved.
  -->
<template>
  <div class="execution">
    <basic-container>
      <avue-crud
        ref="crud"
        :page.sync="page"
        :data="tableData"
        :permission="permissionList"
        :table-loading="tableLoading"
        :option="tableOption"
        @on-load="getList"
        @search-change="searchChange"
        @refresh-change="refreshChange"
        @row-update="handleUpdate"
        @row-save="handleSave"
        @row-del="rowDel"
      >
        <template slot="menuLeft">
          <el-button
            v-show="permissionList.addBtn"
            icon="el-icon-plus"
            type="primary"
            size="small"
            @click="jumpToAdd"
          >新增销量上报</el-button>
        </template>
        <template slot="menuLeft">
          <sales_report_import ref="salesReportImport" style="display: inline" />
        </template>
        <template slot-scope="scope" slot="menu">
          <el-button
            v-show="permissionList.delBtn"
            icon="el-icon-delete"
            type="text"
            size="small"
            plain
            @click="rowDel(scope.row)"
          >删除</el-button>
          <el-button
            icon="el-icon-view"
            type="text"
            size="small"
            plain
            @click="jumpToDetail(scope.row)"
          >详情</el-button>
          <el-button
            v-show="scope.row.reportStatus!=='1'"
            icon="el-icon-edit-outline"
            type="text"
            size="small"
            plain
            @click="jumpToEdit(scope.row)"
            
          >编辑</el-button>
          <el-button
          v-show="scope.row.reportStatus!=='2'"
            type="text"
            size="small"
            icon="el-icon-circle-check"
            @click="reportReview(scope.row)"
          >审核</el-button>
          <!-- <el-button
            v-show="scope.row.reportStatus==='0'"
            icon="el-icon-circle-check"
            type="text"
            size="small"
            plain
            @click="reportReview(scope.row)"
          >审核</el-button>-->
        </template>
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import {
  fetchList,
  addObj,
  putObj,
  deleteReport,
  reportReview
} from "@/api/crm/salesDay";
import { tableOption } from "@/const/crud/crm/reporting/salesDay";
import { mapGetters } from "vuex";
import sales_report_import from "../../../components/crm/excel_import/sales_report_import";

export default {
  name: "salescontract",
  components: {
    sales_report_import
  },
  data() {
    return {
      tableData: [],
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 20 // 每页显示多少条
      },
      tableLoading: false,
      tableOption: tableOption
    };
  },
  computed: {
    ...mapGetters(["permissions"]),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permissions.crm_salescontract_add, false),
        delBtn: this.vaildData(this.permissions.crm_salescontract_del, false),
        editBtn: this.vaildData(this.permissions.crm_salescontract_edit, false)
      };
    }
  },
  created() {
    this.getHeight();
    console.log(this.permissionList.delBtn);
  },
  methods: {
    getHeight() {
      const tableHeight = Math.max(window.innerHeight - 430, 300);
      this.tableOption.height = tableHeight + "px";
    },
    getList(page, params) {
      if (params && params.reportDate && params.reportDate.length > 1) {
        params["beginDate"] = params.reportDate[0];
        params["endDate"] = params.reportDate[1];
      }
      this.tableLoading = true;
      fetchList(
        Object.assign(
          {
            current: page.currentPage,
            size: page.pageSize
          },
          params
        )
      ).then(response => {
        this.tableData = response.data.data.records;
        this.page.total = response.data.data.total;
        this.tableLoading = false;
        // console.log(this.tableData)
      });
    },
    rowDel: function(row) {
      var _this = this;
      this.$confirm("是否确认删除销量上报单号" + row.reportSn, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          const params = {
            reportSn: row.reportSn
          };
          deleteReport(params).then(response => {
            if (response.data.code === 0) {
              _this.$message({
                showClose: true,
                message: "删除成功",
                type: "success"
              });
            }
            _this.getList(_this.page);
          });
        })
        .catch(function(err) {});
    },
    /**
     * @title 数据更新
     * @param row 为当前的数据
     * @param index 为当前更新数据的行数
     * @param done 为表单关闭函数
     *
     **/
    handleUpdate: function(row, index, done) {
      putObj(row).then(data => {
        this.tableData.splice(index, 1, Object.assign({}, row));
        this.$message({
          showClose: true,
          message: "修改成功",
          type: "success"
        });
        done();
        this.getList(this.page);
      });
    },
    /**
     * @title 数据添加
     * @param row 为当前的数据
     * @param done 为表单关闭函数
     *
     **/
    handleSave: function(row, done) {
      addObj(row).then(data => {
        this.tableData.push(Object.assign({}, row));
        this.$message({
          showClose: true,
          message: "添加成功",
          type: "success"
        });
        done();
        this.getList(this.page);
      });
    },
    /**
     * 搜索回调
     */
    searchChange(form, done) {
      this.getList(this.page, form);
      done();
    },
    /**
     * 刷新回调
     */
    refreshChange(info) {
      this.getList(this.page, this.filterForm(info.searchForm));
    },
    jumpToDetail(row) {
      const query = {
        res: {
          reportSn:row.reportSn,
        }
      };
      console.log(query);
      this.$router.push({ path: "/reporting/salesDetail", query: query });
    },
    jumpToAdd() {
      this.$router.push({ path: "/reporting/addSalesDay" });
    },
    jumpToEdit(row) {
      const query = {
        res: {
          reportSn:row.reportSn,
        }
      };
      this.$router.push({ path: "/reporting/editSalesDay", query: query });
    },
    reportReview(row) {
      
      let query = {
        res: {
          reportSn: row.reportSn
        }
      };
      console.log(row);

      this.$router.push({ path: "/reporting/salesDayReview", query: query });
    },
    // reportReview(row) {
      
    //   const _this = this;
    //   this.$prompt("备注信息", {
    //     dangerouslyUseHTMLString: true,
    //     title: '销售上报审核',
    //     confirmButtonText: "通过",
    //     cancelButtonText: "不通过",
    //     inputPlaceholder: "请输入内容",
    //     inputType: "textarea",
    //     rows: "2",
    //     // type: "warning"
    //   })
    //     .then(function() {
    //       const params = {
    //         reportSn: row.reportSn
    //       };
    //       reportReview(params).then(response => {
    //         console.log(response);
            
    //         if (response.data.code === 0 && response.data.data) {
    //           _this.$message({
    //             showClose: true,
    //             message: "审核通过",
    //             type: "success"
    //           });
    //         }
    //         _this.getList(_this.page);
    //       });
    //       // reportReview(params).then(response => {
    //       //   console.log(response);
            
    //       //   if (response.data.code !== 0 && response.data.data==false) {
    //       //     _this.$message({
    //       //       showClose: false,
    //       //       message: "审核bu通过",
    //       //       type: "success"
    //       //     });
    //       //   }
    //       //   _this.getList(_this.page);
    //       // });
    //     })
    //     .catch(function(err) {});
    // }
  }
};
</script>

<style lang="scss" scoped>
</style>
